﻿using System;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Globalization;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace Eurocalculator
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            // Housekeeping
            InitializeComponent();
            _inputNumbers = "";
            // For reference - CultureInfo.CurrentCulture... is neccesary because some locales use "." as seperator others ","
            EekDisplay.Text = ("0" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + "00");
            EurDisplay.Text = ("0" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + "00");
            buttonDot.Content = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
        }

        // Setting up some variables 
        private bool _euro = true;
        private string _inputNumbers;
        private string _inProgress;
        private string _display;
        private double _lenghtOfDisplay = 6;
        private double _lenghtOfDisplayMax = 12;
        private double _eurCourse = 15.6466;
        private double _conversion;
        private double _sumOfNumbers;

        #region keypad

        private void keypad_Click(object sender, RoutedEventArgs e)
        {
            // What number was pressed?
            Button keyInput = sender as Button;
            _inProgress = _inputNumbers + keyInput.Content;
            _inputNumbers = _inProgress;
            if (_lenghtOfDisplay < _inputNumbers.Length)
            {
                EekDisplay.FontSize = 40;
                EurDisplay.FontSize = 40;
            }
            _sumOfNumbers = Convert.ToDouble(_inputNumbers);


            // Lets check if its Euro or Eek that was inputed
            if (_euro == true)
            {
                EekDisplay.Text = _inputNumbers;

                _conversion = _sumOfNumbers * _eurCourse;
                _display = Convert.ToString(Math.Round(_conversion, 2));

                if (_lenghtOfDisplayMax < _inputNumbers.Length)
                {
                    EurDisplay.FontSize = 32;
                    EurDisplay.FontSize = 32;
                }

                EurDisplay.Text = _display;
            }
            else
            {
                EurDisplay.Text = _inputNumbers;

                _conversion = _sumOfNumbers / _eurCourse;
                _display = Convert.ToString(Math.Round(_conversion, 2));

                if (_lenghtOfDisplayMax < _inputNumbers.Length)
                {
                    EurDisplay.FontSize = 32;
                    EurDisplay.FontSize = 32;
                }

                EekDisplay.Text = _display;
            }
        }

        // Implementing a DOT
        private void buttonDot_Click(object sender, RoutedEventArgs e)
        {
            string dot = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
            if (_inputNumbers.Contains(dot))
            {
                MessageBox.Show("You already have a separator there!");
            }
            else
            {
                _inProgress = _inputNumbers + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                _inputNumbers = _inProgress;
                if (_lenghtOfDisplay < _inputNumbers.Length)
                {
                    EekDisplay.FontSize = 46;
                }
                EekDisplay.Text = _inputNumbers;
            }

        }

        // Clear all button
        private void buttonCe_Click(object sender, RoutedEventArgs e)
        {
            _inProgress = "0" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + "00";
            _inputNumbers = "";
            EekDisplay.FontSize = 56;
            EekDisplay.Text = _inProgress;
            EurDisplay.FontSize = 56;
            EurDisplay.Text = _inProgress;
        }

        #endregion keypad

        // Eur / Eek button
        private void convert_Click(object sender, EventArgs e)
        {
            if (_euro == true)
            {
                _euro = false;
                Brush eekColor = (Brush)Application.Current.Resources["PhoneAccentBrush"];
                EekIcon.Foreground = eekColor;
                // Lets indicate color of active currency 
                Brush eurColor = (Brush)Application.Current.Resources["PhoneContrastBackgroundBrush"];
                EurIcon.Foreground = eurColor;
                
            }
            else
            {
                _euro = true;
                Brush eurColor = (Brush)Application.Current.Resources["PhoneAccentBrush"];
                EurIcon.Foreground = eurColor;
                // Lets indicate color of active currency 
                Brush eekColor = (Brush)Application.Current.Resources["PhoneContrastBackgroundBrush"];
                EekIcon.Foreground = eekColor;
            }

            // Just Null all to make visual feedback to user
            _inProgress = "0" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + "00";
            _inputNumbers = "";
            EekDisplay.FontSize = 56;
            EekDisplay.Text = _inProgress;
            EurDisplay.FontSize = 56;
            EurDisplay.Text = _inProgress;
        }

        // About page
        private void about_Click(object sender, EventArgs e)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }
    }
}
